<template>
    <div class="page_1">
        <div class="top_box">
            <el-input style="width:200px" placeholder="请输入部门名称" v-model="keyword"></el-input>
            <el-button type="warning" style="margin-left:20px" icon="el-icon-search" size="small" @click="getInfo">搜索</el-button>
            <el-button style="margin-left:20px" size="small" @click="reset" type="primary">重置</el-button>
            <el-button type="warning" icon="el-icon-plus" size="small" @click="add">添加部门</el-button>
        </div>
        <!-- 表格 -->
        <el-table :data="data" style="width: 100%" border row-key="id" :indent="60" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">

            <el-table-column prop="name" label="部门名称" sortable align="center">
            </el-table-column>
            <el-table-column prop="sort" label="排序" sortable align="center">
            </el-table-column>
            <el-table-column label="创建时间" sortable align="center">
                <template slot-scope="{row}">
                    {{ comTime(row.create_time) }}
                </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="300">
                <template slot-scope="{ row }">
                    <el-button type="warning" @click="edit(row)">编辑</el-button>
                    <el-button type="danger" @click="del(row.id)">删除</el-button>
                    <el-button v-if="row.pid == 0" type="info" @click="addzi(row.id)">添加子部门</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 表单 -->
        <el-dialog :visible.sync="dialogVisible" :title="formTitle" append-to-body width="400px" @close="formReset">
            <el-form ref="form" :rules="rules" :model="form" label-width="80px">
                <el-row type="flex" :gutter="10">
                    <el-col :span="24">
                        <el-form-item label="部门名称" prop="name">
                            <el-input v-model="form.name" placeholder="请输入部门名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="排序" prop="sort">
                            <el-input v-model="form.sort" placeholder="数值越大越靠前，默认数值100"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" justify="center">
                    <el-button type="primary" @click="initiate()">确定</el-button>
                    <el-button @click="quxiao" type="primary">取消</el-button>
                </el-row>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import {
    get, del, post
} from "@/assets/js/http.js";
export default {
    data() {
        return {
            keyword: "",
            data: [],
            dialogVisible: false,
            formTitle: "添加部门",
            pid: "",
            form: {
                name: "",
                sort: ""
            },
            rules: {
                name: [
                    { required: true, message: '请输入部门名称', trigger: 'blur' },
                ],
                sort: [
                    { required: true, message: '请输入排序数值', trigger: 'blur' },
                ],
            }
        }
    },
    methods: {
        getInfo() {
            post({
                url: "/admin/answer.Road/department_list",
                loading: true,
                data: {
                    keyword: this.keyword
                }
            }).then(res => {
                this.data = res
            })
        },
        // 添加部门
        add() {
            this.formTitle = "添加部门"
            this.dialogVisible = true
        },
        initiate() {
            let data = JSON.parse(JSON.stringify(this.form))
            if (this.pid) {
                data.pid = this.pid
            }
            post({
                url: "/admin/answer.Road/department_edit",
                data,
                loading: true,
                successTip: true,
            }).then(res => {
                this.dialogVisible = false
                this.getInfo()
            })
        },
        // 修改部门
        edit(row) {
            this.formTitle = "修改部门"
            this.dialogVisible = true
            this.form = row
        },
        // 删除部门
        del(id) {
            this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                post({
                    url: "admin/answer.Road/department_del",
                    data: { id },
                    loading: true,
                    successTip: true,
                }).then(res => {
                    this.getInfo()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 取消
        quxiao() {
            this.dialogVisible = false
            this.formReset
        },
        formReset() {
            this.form = Object.assign({}, this.$options.data().form)
        },
        addzi(id, row) {
            console.log(row);
            this.dialogVisible = true
            this.formTitle = "添加子部门"
            this.pid = id
        },
        // 重置
        reset() {
            this.keyword = ''
            this.getInfo()
        },
    },
    computed: {
        comTime() {
            return (tiem) => {
                const d = new Date(tiem * 1000);
                let nian = d.getFullYear();
                let yue = d.getMonth();
                let ri = d.getDate();
                return `${nian}-${yue + 1}-${ri}`;
            };
        },
    },
    mounted() {
        this.getInfo()
    },
}
</script>

<style></style>